package com.panda.pandawash.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.StrongBoxBean;
import com.panda.pandawash.util.HttpUtil;

/**
 * Created by gaowf on 2017/2/9.
 */

public class EnterSafeDialog extends Dialog{
    private static String TAG = "API_TAG";

    public EnterSafeDialog(Context context) {
        super(context);
    }

    public EnterSafeDialog(Context context, int theme) {
        super(context, theme);
    }


    public static class Builder {
        private Context context;

        private Button cancelButton;
        private Button confirmButton;
        private EditText inputPassword;
        private String getPassword;

        private String JUMPTOFORGETGACTIVITY = "com.panda.pandawash.SafeForgetPsdActivity";
        private String JUMPTOSAFEBOXACTIVITY = "com.panda.pandawash.SafeBoxActivity";
        private EnterSafeDialog dialog = null;
        public Builder(Context context) {
            this.context = context;
        }

        public interface EnterInfoCallback {
            public void onEDialogOKbuttonClick(String password);
        }
        public  EnterInfoCallback enterInfoCallback = null;

        public void setEnterInfoCallback(EnterInfoCallback eDCallback){
            enterInfoCallback = eDCallback;
        }

        public EnterSafeDialog create() {
            final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialog = new EnterSafeDialog(context, R.style.SetSafeDialogStyle);//SetSafeDialogStyle
            View layout = inflater.inflate(R.layout.dialog_enter_safe, null);

            dialog.addContentView(layout, new WindowManager.LayoutParams(
                    android.view.ViewGroup.LayoutParams.WRAP_CONTENT
                    , android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setContentView(layout);

            inputPassword = (EditText) layout.findViewById(R.id.input_safe_box_psd);

            TextView forgetSafePsd = (TextView) layout.findViewById(R.id.forget_safe_box_psd);
            forgetSafePsd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                    Intent intent = new Intent();
                    intent.setAction(JUMPTOFORGETGACTIVITY);
                    intent.putExtra("msg", "发送忘记密码广播成功！");
                    context.sendBroadcast(intent);
                }
            });


            cancelButton = (Button) layout.findViewById(R.id.cancel_psd_cancel);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.d("gwf_add", "click cancel in dialog");
                    dialog.dismiss();
                }
            });

            confirmButton = (Button) layout.findViewById(R.id.enter_psd_confirm);
            confirmButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    SharedPreferences sp = context.getSharedPreferences("safe_box", Context.MODE_PRIVATE);
//                    String safeBox = sp.getString("safe_box_dialog", null);
                    getPassword = inputPassword.getText().toString();
                    if (TextUtils.isEmpty(getPassword)) {
                        showMessage(context.getString(R.string.safe_box_password));
                        return;
                    }
                    if (!getPassword.trim().equals(getPassword)) {
                        showMessage(context.getString(R.string.registe_password_have_space));
                        return;
                    }
                    enterInfoCallback.onEDialogOKbuttonClick(getPassword);

//                    final  View progress = inflater.inflate(R.layout.dialog_progress, null);
//                    dialog.setContentView(progress);
                    dialog.dismiss();
                   // loginStrongBoxPsw();

                }
            });

            return dialog;
        }


        private void loginStrongBoxPsw(){//暂时没用，点击OK直接调用比较慢，上面尝试回调，效果差不多，发消息也很慢
            String account = CDApplication.getIntance().getUserInfo().getUseraccount();
            String password = inputPassword.getText().toString().trim();
            StrongBoxBean strongBoxBean = new StrongBoxBean();
            strongBoxBean.setSecretpassword(password);
            HttpUtil.loginStrongBoxPSW(new LoginSafeBoxCallback(),account,account,strongBoxBean);
        }

        private class LoginSafeBoxCallback extends AsyncHttpResponseHandler {
            @Override
            public void onSuccess(int statusCode, String content) {
                super.onSuccess(statusCode, content);
                Log.d(TAG, "LoginSafeBoxCallback statusCode = " + statusCode);
                Log.d(TAG, "LoginSafeBoxCallback onSuccess = " + content);
                if (statusCode == 200) {
                    HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, null);
                    Log.d(TAG, "resultBean = " + resultBean);
                    if (resultBean != null) {
                        String code = resultBean.getCode();
                        String message = resultBean.getMessage();
                        Log.d(TAG, "code = " + code);
                        if(code.equals("0")){
                            showMessage(context.getString(R.string.safe_box_login_success));

                            Intent intent = new Intent();
                            intent.setAction(JUMPTOSAFEBOXACTIVITY);
                            intent.putExtra("msg", "发送确定进入保险箱广播成功！");
                            context.sendBroadcast(intent);

                        }else if(code.equals("99")){
                            showMessage(message);
                        }
                    }
                } else {
                    showMessage(context.getString(R.string.safe_box_login_fail));
                }
                //dialog.dismiss();
            }

            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
                Log.d(TAG, "onFailure = " + error);
                if (error.toString().contains("refused")) {

                }
               // dialog.dismiss();
            }

        }

        private void showMessage(String string) {
            Toast toast = Toast.makeText(context.getApplicationContext(), string, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }

    }



}
